import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { IElecBicycleAdmin } from 'app/shared/model/elec-bicycle-admin.model';
import { ElecBicycleAdminService } from './elec-bicycle-admin.service';

@Component({
    selector: 'jhi-elec-bicycle-admin-update',
    templateUrl: './elec-bicycle-admin-update.component.html'
})
export class ElecBicycleAdminUpdateComponent implements OnInit {
    private _elecBicycleAdmin: IElecBicycleAdmin;
    isSaving: boolean;

    constructor(private elecBicycleAdminService: ElecBicycleAdminService, private activatedRoute: ActivatedRoute) {}

    ngOnInit() {
        this.isSaving = false;
        this.activatedRoute.data.subscribe(({ elecBicycleAdmin }) => {
            this.elecBicycleAdmin = elecBicycleAdmin;
        });
    }

    previousState() {
        window.history.back();
    }

    save() {
        this.isSaving = true;
        if (this.elecBicycleAdmin.id !== undefined) {
            this.subscribeToSaveResponse(this.elecBicycleAdminService.update(this.elecBicycleAdmin));
        } else {
            this.subscribeToSaveResponse(this.elecBicycleAdminService.create(this.elecBicycleAdmin));
        }
    }

    private subscribeToSaveResponse(result: Observable<HttpResponse<IElecBicycleAdmin>>) {
        result.subscribe((res: HttpResponse<IElecBicycleAdmin>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
    }

    private onSaveSuccess() {
        this.isSaving = false;
        this.previousState();
    }

    private onSaveError() {
        this.isSaving = false;
    }
    get elecBicycleAdmin() {
        return this._elecBicycleAdmin;
    }

    set elecBicycleAdmin(elecBicycleAdmin: IElecBicycleAdmin) {
        this._elecBicycleAdmin = elecBicycleAdmin;
    }
}
